package com.jandan.ui.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Function;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.Tool;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
import com.jandan.ui.client.service.StrangeWordService;
import com.jandan.ui.client.service.StrangeWordServiceAsync;
import com.jandan.ui.model.StrangeWordGroupInfo;

public class StrangeWordPanel extends TreePanel {
	private Menu menu;
	private TreeNode ctxNode;
	private ILessonOperator lessonOperate;

	public StrangeWordPanel(ILessonOperator lessonOperator) {
		this.lessonOperate = lessonOperator;
		this.setTitle("我的生词本");

		this.addTool(new Tool(Tool.REFRESH, new Function() {
			public void execute() {
				// MessageBox.alert("刷新");
				// lessonOperate.addStrangeWord();
				lessonOperate.refeshStrangeWordPanel();
			}
		}, "刷新"));

		this.addTool(new Tool(Tool.DOWN, new Function() {
			public void execute() {
				lessonOperate.addStrangeWord();
			}
		}, "添加生词"));

		this.addTool(new Tool(Tool.HELP, new Function() {
			public void execute() {
				lessonOperate.browseStrangeWord();
			}
		}, "查看所有生词"));

		this.setRootNode(new TreeNode("my strange word"));
		this.setRootVisible(false);

		this.addListener(new TreePanelListenerAdapter() {
			public void onContextMenu(TreeNode node, EventObject e) {
//				int[] xy = e.getXY();
				// MessageBox.alert(node.getId());
				showContextMenu(node, e);
			}
		});
		this.createComponent();
	}

	private void createComponent() {
		StrangeWordServiceAsync strangeWordService = StrangeWordService.Util
				.getInstance();

		strangeWordService
				.getStrangeWordGroupInfoList(new AsyncCallback<List<StrangeWordGroupInfo>>() {

					public void onFailure(Throwable t) {
						// TODO Auto-generated method stub
						System.out.println(t.getMessage());
					}

					public void onSuccess(
							List<StrangeWordGroupInfo> strangeWordGroupInfoList) {
						// TODO Auto-generated method stub
						if (strangeWordGroupInfoList != null
								&& strangeWordGroupInfoList.size() > 0) {
							Iterator<StrangeWordGroupInfo> it = strangeWordGroupInfoList
									.iterator();
							StrangeWordGroupInfo s = null;
							while (it.hasNext()) {
								s = it.next();
								addStrangeWordGroup(s);
							}
						}
					}
				});
	}

	private void addStrangeWordGroup(StrangeWordGroupInfo s) {
		if (s == null) {
			return;
		}

		TreeNode root = this.getRootNode();
		final int groupNumber = s.getGroupNumber();

		TreeNode groupNode = new TreeNode("第" + (s.getGroupNumber() + 1) + "组("
				+ s.getStartWord() + " - " + s.getEndWord() + ")("
				+ s.getTotalWordCount() + ")");
		groupNode.setExpanded(false);
		groupNode.setId("" + groupNumber);

		groupNode.addListener(new TreeNodeListenerAdapter() {
			@Override
			public void onClick(Node node, EventObject e) {
				// TODO Auto-generated method stub
				lessonOperate.studyStrangeWord(groupNumber);
			}
		});

		root.appendChild(groupNode);
	}

	private void showContextMenu(final TreeNode node, EventObject e) {
//		final String nodeId = node.getId();
		if (menu == null) {
			menu = new Menu();
			Item studyItem = new Item("学习", new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
//					String id = ctxNode.getId();
				}
			});
			studyItem.setId("disable-item");
			menu.addItem(studyItem);

			Item browseItem = new Item("浏览", new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
				}
			});
			browseItem.setId("browse_item");
			menu.addItem(browseItem);

		}

		if (ctxNode != null) {
			ctxNode = null;
		}
		ctxNode = node;

		menu.showAt(e.getXY());
	}

	private List<StrangeWordGroupInfo> getStrangeWordGroupList() {
		List<StrangeWordGroupInfo> list = new ArrayList<StrangeWordGroupInfo>();

		StrangeWordGroupInfo s1 = new StrangeWordGroupInfo();
		s1.setStartWord("abolish");
		s1.setEndWord("advent");
		s1.setLearnedWordCount(10);
		s1.setTotalWordCount(20);
		s1.setGroupNumber(0);

		StrangeWordGroupInfo s2 = new StrangeWordGroupInfo();
		s2.setStartWord("abolish");
		s2.setEndWord("advent");
		s2.setLearnedWordCount(10);
		s2.setTotalWordCount(20);
		s2.setGroupNumber(1);

		StrangeWordGroupInfo s3 = new StrangeWordGroupInfo();
		s3.setStartWord("abolish");
		s3.setEndWord("advent");
		s3.setLearnedWordCount(10);
		s3.setTotalWordCount(20);
		s3.setGroupNumber(2);

		list.add(s1);
		list.add(s2);
		list.add(s3);

		return list;
	}

	public int getTotalGroupCount() {
		return this.getRootNode().getChildNodes().length;
	}

	public void refresh(List<StrangeWordGroupInfo> strangeWordGroupInfoList) {
		// TODO Auto-generated method stub
		if (strangeWordGroupInfoList != null
				&& strangeWordGroupInfoList.size() > 0) {
			TreeNode root = this.getRootNode();
			Node[] nodes = root.getChildNodes();
			for (int i = 0; i < nodes.length; i++) {
				root.removeChild(nodes[i]);
			}

			StrangeWordGroupInfo group = null;
			for (Iterator<StrangeWordGroupInfo> it = strangeWordGroupInfoList
					.iterator(); it.hasNext();) {
				group = it.next();
				this.addStrangeWordGroup(group);
			}
		}
	}
}
